package com.coocaa.coolink_tvos_mobile_demo.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import java.util.UUID;

public class UniqueDeviceID {
    private static final String DEVICE_ID_KEY = "device_id";
    private static String deviceId;

    public static synchronized String getDeviceId(Context context) {
        if (deviceId == null) {
            SharedPreferences preferences = context.getSharedPreferences("unique_device_id", Context.MODE_PRIVATE);
            deviceId = preferences.getString(DEVICE_ID_KEY, null);
            if (deviceId == null) {
                deviceId = generateDeviceId(context);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString(DEVICE_ID_KEY, deviceId);
                editor.apply();
            }
        }
        return deviceId;
    }

    private static String generateDeviceId(Context context) {
        String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if ("9774d56d682e549c".equals(androidId)) {
            // If the Android ID is 9774d56d682e549c, it's probably a device without a real Android ID,
            // so we generate a unique ID as a fallback.
            return UUID.randomUUID().toString();
        } else {
            return androidId;
        }
    }
}